const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const router = new Router()
const usersRouter = new Router({ prefix: '/users' })

// 中间件
router.get('/', (ctx) => {
    ctx.body = '这是主页'
})
usersRouter.get('/', ctx => {
    ctx.body = [{ name: '李雷' }, { name: '韩梅梅' }]
})
// 创建
usersRouter.post('/', ctx => {
    ctx.body = { name: '李雷' }
})
usersRouter.get('/:id', ctx => {
    ctx.body = { name: '李雷' }
})
// 修改
usersRouter.put('/', ctx => {
    ctx.body = { name: '李雷2' }
})
usersRouter.delete('/', ctx => {
    ctx.status = 204
})
app.use(router.routes())
app.use(usersRouter.routes())
// 支持所有接口的options请求
app.use(router.allowedMethods())
app.listen(3000)